package cn.iocoder.yudao.module.bpm.controller.admin.oaapply;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.bpm.controller.admin.oaapply.vo.*;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oaapply.OaApplyDO;
import cn.iocoder.yudao.module.bpm.service.oaapply.OaApplyService;

@Tag(name = "管理后台 - OA 请假申请")
@RestController
@RequestMapping("/bpm/oa-apply")
@Validated
public class OaApplyController {

    @Resource
    private OaApplyService oaApplyService;

    @PostMapping("/create")
    @Operation(summary = "创建OA 请假申请")
    @PreAuthorize("@ss.hasPermission('bpm:oa-apply:create')")
    public CommonResult<Long> createOaApply(@Valid @RequestBody OaApplySaveReqVO createReqVO) {
        return success(oaApplyService.createOaApply(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新OA 请假申请")
    @PreAuthorize("@ss.hasPermission('bpm:oa-apply:update')")
    public CommonResult<Boolean> updateOaApply(@Valid @RequestBody OaApplySaveReqVO updateReqVO) {
        oaApplyService.updateOaApply(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除OA 请假申请")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('bpm:oa-apply:delete')")
    public CommonResult<Boolean> deleteOaApply(@RequestParam("id") Long id) {
        oaApplyService.deleteOaApply(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得OA 请假申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bpm:oa-apply:query')")
    public CommonResult<OaApplyRespVO> getOaApply(@RequestParam("id") Long id) {
        OaApplyDO oaApply = oaApplyService.getOaApply(id);
        return success(BeanUtils.toBean(oaApply, OaApplyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得OA 请假申请分页")
    @PreAuthorize("@ss.hasPermission('bpm:oa-apply:query')")
    public CommonResult<PageResult<OaApplyRespVO>> getOaApplyPage(@Valid OaApplyPageReqVO pageReqVO) {
        PageResult<OaApplyDO> pageResult = oaApplyService.getOaApplyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OaApplyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出OA 请假申请 Excel")
    @PreAuthorize("@ss.hasPermission('bpm:oa-apply:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOaApplyExcel(@Valid OaApplyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OaApplyDO> list = oaApplyService.getOaApplyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "OA 请假申请.xls", "数据", OaApplyRespVO.class,
                        BeanUtils.toBean(list, OaApplyRespVO.class));
    }

}